//
//  ViewController.m
//  XAIDemo
//
//  Created by yanghao on 2018/4/22.
//  Copyright © 2018年 yanghao. All rights reserved.
//

#import "ViewController.h"
#import <XAI.h>
#import <XAIAPSNUtil.h>
#import <XAILUIDUtil.h>

#define kHOST @"47.97.198.133"
#define kPORT 9001
#define kUSER @"13550075599"
#define kPWD  @"123456"

@interface ViewController () <XAIDelegate>
@property (nullable, nonatomic, strong) XAI *xaiService;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.xaiService = [[XAI alloc]initWithHost:kHOST port:kPORT];
    self.xaiService.delegate = self;
    [self.xaiService startWithAccount:kUSER password:kPWD success:^(XAIConnectReturnCode code) {
        if (code == XAIConnectionAccepted) {
            NSLog(@"登录成功");
            [self.xaiService sendMyDev:0x0000001 verson:0x00010000 sleep:false descritpion:@"bob的手机"];
            [self.xaiService sendMyDeviceConnection:true];
            [self.xaiService sendMyDeviceName:@"bob"];
            [self.xaiService sendMyDevicePower:100];
        }
    } error:^(XAIConnectReturnCode code) {
        NSLog(@"登录失败");
    }];
    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - XAIDelegate

- (void)deviceDetected:(XAI *)xai deviceApsn:(XAITYPEAPSN)deviceApsn deviceLuid:(XAITYPELUID)deviceLuid deviceType:(XAITYPEDEVICETYPE)deviceType version:(XAITYPEVERSION)version description:(NSString *)description {
    
    if ([XAILUIDUtil isServerAP:deviceLuid] && [XAIAPSNUtil isGateway:deviceApsn]) {
        NSLog(@"收到一个网关 apsn=%08x luid= %016llx description= %@",deviceApsn,deviceLuid,description);
    } else if ([XAILUIDUtil iSMobile:deviceLuid]  && [XAIAPSNUtil isCloud: deviceApsn]) {
        NSString *mobile = [XAI luidToMobile:deviceLuid];
        NSLog(@"收到一个好友 apsn= %08x mobile= %@ description= %@",deviceApsn,mobile,description);
    } else if ([XAILUIDUtil isNode:deviceLuid] && [XAIAPSNUtil isGateway:deviceApsn] && ![XAIAPSNUtil isCloud:deviceApsn]) {
         NSLog(@"收到一个节点设备 apsn= %08x luid= %016llx description= %@",deviceApsn,deviceLuid,description);
    }
}

- (void)deviceRemoved:(XAI *)xai
           deviceApsn:(XAITYPEAPSN)deviceApsn
           deviceLuid:(XAITYPELUID)deviceLuid {
    NSLog(@"设备被删除 apsn = %08x luid = %016llx",deviceApsn, deviceLuid);
}

- (void)linkRuleDetected:(XAI *)xai
                  linkID:(XAITYPEID)linkID
             gatewayApsn:(XAITYPEAPSN)gatewayApsn
                linkName:(NSString *)linkName
                  active:(BOOL)active
              conditions:(NSArray<XAILinkItemDes *> *)conditions
                 actions:(NSArray<XAILinkItemDes *> *)actions {
    NSLog(@"link detected");
}

- (void)linkRuleRemoved:(XAI *)xai gatewayApsn:(XAITYPEAPSN)gatewayApsn linkID:(XAITYPEID)linkID {
    //联动删除
    NSLog(@"link removed");
}



- (void)CMDMessageRecived:(XAI *)xai
               deviceApsn:(XAITYPEAPSN)deviceApsn
               deviceLuid:(XAITYPELUID)deviceLuid
                timestamp:(XAITYPETIME)timestamp
                    cmdID:(XAITYPEID)cmdID
                    datas:(NSArray<XAITLV *> *)datas {
    
}

- (void)IMMessageRecived:(XAI * _Nonnull)xai deviceApsn:(XAITYPEAPSN)deviceApsn deviceLuid:(XAITYPELUID)deviceLuid timestamp:(XAITYPETIME)timestamp datas:(NSArray<XAITLV *> * _Nonnull)datas {
    
}


- (void)adminDetected:(XAI * _Nonnull)xai adminApsn:(XAITYPEAPSN)apsn adminLuid:(XAITYPELUID)luid {
    
}


- (void)jpushRuleDetected:(XAI * _Nonnull)xai gatewayApsn:(XAITYPEAPSN)gatewayApsn registerID:(NSString * _Nonnull)registerID configs:(NSArray<XAIJPushRule *> * _Nonnull)configs enable:(BOOL)enable {
    
}

- (void)jpushRuleDetected:(XAI *)xai gatewayApsn:(XAITYPEAPSN)gatewayApsn {

}

- (void)localGatewayDetected:(XAI * _Nonnull)xai pack:(XAIBroadcastPack * _Nonnull)pack {
    
}


- (void)statusMessageRecived:(XAI * _Nonnull)xai deviceApsn:(XAITYPEAPSN)deviceApsn deviceLuid:(XAITYPELUID)deviceLuid triggerApsn:(XAITYPEAPSN)triggerApsn triggerLuid:(XAITYPELUID)triggerLuid timestamp:(XAITYPETIME)timestamp statusID:(XAITYPEID)statusID datas:(NSArray<XAITLV *> * _Nullable)datas isHistory:(BOOL)isHistory {
    if (datas == nil || datas.count == 0) {
        NSLog(@"收到的数据为空");
        return;
    }
    switch (statusID) {
        case 0xff:
        {
            if (isHistory) {
                
            } else {
                XAITLV* data = datas.firstObject;
                const void* bytes = [data getPayloadbyte];
                if (bytes == NULL) {
                    return;
                }
                
                NSString* name = [[NSString alloc] initWithBytes:bytes length:data.payloadlen encoding:NSUTF8StringEncoding];
                NSLog(@"设备名字%@",name);
            }
        }
            break;
        case 0xfe:
        {
            if (isHistory) {
                
            } else {
                XAITLV* data = datas.firstObject;
                uint8_t status = 0;
                memcpy(&status, [data getPayloadbyte], sizeof(uint8_t));
                if (status == 0) {
                    NSLog(@"设备离线");
                } else if (status == 1) {
                    NSLog(@"设备在线");
                } else if (status == 2) {
                    NSLog(@"设备休眠");
                }
            }
        }
            break;
        case 0xfd:
        {
            if (isHistory) {
                
            } else {
                XAITLV* data = datas.firstObject;
                uint8_t popver = 0;
                memcpy(&popver, [data getPayloadbyte], sizeof(uint8_t));
                NSLog(@"设备电量%d",popver);
            }
        }
            break;
        case 0x01:
        {
            if ([XAILUIDUtil isServerAP:deviceLuid] && [XAIAPSNUtil isGateway:deviceApsn]) {
                XAITLV* data = datas.firstObject;
                uint8_t linkID = 0;
                memcpy(&linkID, [data getPayloadbyte], sizeof(uint8_t));
                NSLog(@"%08x当前的场景是%hhu",deviceApsn,linkID);
            }
        }
            break;
        case 0x02:
        {
            if ([XAILUIDUtil isServerAP:deviceLuid] && [XAIAPSNUtil isGateway:deviceApsn]) {
                XAITLV* data = datas.firstObject;
                uint64_t newdevLuid = 0;
                memcpy(&newdevLuid, [data getPayloadbyte], sizeof(newdevLuid));
                newdevLuid = CFSwapInt64(newdevLuid);
                NSLog(@"设备请求入网 apsn= %08x luid= %016llx",deviceApsn,newdevLuid);
            }
        }
        default:
            break;
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
